package com.lu.nio.client;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.lu.nio.ChannelAdapter;
import lombok.extern.slf4j.Slf4j;

import java.nio.channels.Selector;
import java.util.Date;

/**
 * @program lu-netty
 * @description:
 * @author: zhanglu
 * @create: 2022-06-10 16:38:00
 */
@Slf4j
public class NioClientHandler extends ChannelAdapter {

    public NioClientHandler(Selector selector, String charset) {
        super(selector, charset);
    }

    public void writeAndFlush(Object obj){
        this.ctx.writeAndFlush(obj);
    }

    @Override
    protected void channelActive() {
        this.ctx.writeAndFlush("hi！小鲁哥~");
    }

    @Override
    protected void channelRead(Object msg) {
        String currentDate = DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN);
        log.info(StrUtil.format("{} --- 接收消息：{}", currentDate, msg));
    }

}
